package com.rdxer.service;

import com.rdxer.core.ex.ObjectEx;
import com.rdxer.dataview.AnswerDV;
import com.rdxer.dataview.CommitAnswerSheetDV;
import com.rdxer.core.utils.IdWorker;
import com.rdxer.model.*;
import com.rdxer.repository.AnswerOptionRepository;
import com.rdxer.repository.AnswerRepository;
import lombok.Data;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Department 服务层
 */
@Service
@Data
@Getter
public class AnswerService implements CRUDPageServiceInterface<Answer, String> {


    @Autowired
    private AnswerRepository repository;
    @Autowired
    private AnswerOptionRepository optionRepository;

    @Autowired
    private IdWorker idWorker;

    public AnswerDV findAnswerDV(String answerSheetId, String paperQuestionRelationId) {
        Answer answer = findAnswer(answerSheetId, paperQuestionRelationId);
        AnswerDV dv = AnswerDV.builder().build();

        BeanUtils.copyProperties(answer,dv);

        List<AnswerOption> list = findAnswerOption(answer.getId());

        dv.setOptionList(list);

        return dv;
    }
    public Answer findAnswer(String answerSheetId, String paperQuestionRelationId) {
        List<Answer> list = repository.findAllByAnswerSheetIdAndPaperQuestionId(answerSheetId, paperQuestionRelationId);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }
    public Answer setAnswer(AnswerSheet answerSheet, CommitAnswerSheetDV dv) {

        Answer answer = Answer.builder()
                .answerSheetId(answerSheet.getId())
                .paperQuestionId(dv.getId())
                .build();

        String type = dv.getQuestion().getType();
        switch (Question.Type.valueOf(type)) {
            case FreeResponse:
            case Completion:
                answer.setContent(ObjectEx.get(()->dv.getV().toString()));
                break;
            default:
                break;
        }
        save(answer);

        switch (Question.Type.valueOf(type)) {
            case Single:
                ;
            case Judge:
                addAnswerOption(answerSheet.getId(),answer.getId(), ObjectEx.get(()->dv.getV().toString()));
                break;
            case Multi:
                ;
                List<String> list = ObjectEx.get(()->(List<String>) dv.getV(),new ArrayList<>());
                for (String id : list) {
                    addAnswerOption(answerSheet.getId(),answer.getId(), id);
                }
                break;
            default:
                break;
        }

        return answer;
    }



    public List<AnswerOption> findAnswerOption(String answerId) {
        List<AnswerOption> list = optionRepository.findAllByAnswerId(answerId);
        return list;
    }
    @Autowired
    QuestionOptionService questionOptionService;
    public void addAnswerOption(String answerSheetId,String answerId, String optionId) {
        if (optionId == null){
            return;
        }
        QuestionOption byId = questionOptionService.findById(optionId);
        AnswerOption op = AnswerOption.builder()
                .id(idWorker.nextIdHex())
                .answerId(answerId)
                .answerSheetId(answerSheetId)
                .questionOptionId(optionId)
                .correct(byId.getCorrect())
                .build();
        optionRepository.save(op);
    }
    @Transactional
    public void clearByAnswerSheet(String answerSheetId) {
        repository.deleteAllByAnswerSheetId(answerSheetId);
        optionRepository.deleteAllByAnswerSheetId(answerSheetId);
    }

    public List<Answer> findByAnswerSheetId(String answerSheetId) {
        return repository.findAllByAnswerSheetId(answerSheetId);
    }
}
